home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
HyperCard
/
RTF XCMDs 1.3
/
FieldToRTF.c
next >
Wrap
C/C++ Source or Header
|
1993-03-29
|
6KB
|
270 lines
/*
* This software is copyright 1992 by Robert Morris.
* You may freely redistribute this software as shareware
* if you do so in the same form as you got it. If you find
* this software useful, please send $12 to:
* Robert Morris
* P.O. Box 1044
* Harvard Square Station
* Cambridge, MA 02238
* ecognome@aol.com
* If you incorporate any of this software in any kind of
* commercial product, please send $2 per copy distributed
* to the above address.
*/
/*
* FieldToStyled(cardflag, fieldname)
* yields RTF for the field.
*/
#include <HyperXCmd.h>
#include <stdlib.h>
#include <ctype.h>
#include "SetUpA4.h"
#include <string.h>
#include "lists.h"
int sprintf(char *, const char *, ...);
int sscanf(const char *, const char *, ...);
Handle HStr(char *);
int **addfonttable();
char *fontfamily();
pascal void
main(paramPtr)
XCmdPtr paramPtr;
{
char **in;
int run, len, c;
long i, inlen;
TEHandle teh;
char fieldname[256], tmp[256], fname[256];
Boolean cardFld;
STElement *stp;
TEStyleHandle sh;
struct list out;
int **fonttable, nfonts;
char *dummy;
int fieldID, fieldNum;
RememberA0();
SetUpA4();
dummy = "Copyright 1991 by Differential Development.";
if(paramPtr->paramCount != 2){
paramPtr->returnValue = HStr("error : usage FieldToRTF(cardflag, fieldname)\rCopyright 1992 Robert Morris.");
RestoreA4();
return;
}
strncpy(fieldname, *(paramPtr->params[1]), sizeof(fieldname)-1);
fieldname[253] = '\0'; /* seems to be the max field name length */
if(strcmp(*(paramPtr->params[0]), "false") == 0)
cardFld = FALSE;
else
cardFld = TRUE;
if(isdigit(fieldname[0])){
fieldID = atoi(fieldname);
fieldNum = 0;
} else {
CtoPstr(fieldname);
fieldID = fieldNum = 0;
}
teh = GetFieldTE(paramPtr, cardFld, fieldID, fieldNum,
fieldID ? 0 : (StringPtr)fieldname);
if(teh == 0 || paramPtr->result != xresSucc){
paramPtr->returnValue = HStr("error : no such field");
RestoreA4();
return;
}
sh = GetStylHandle(teh);
if(sh == 0){
TEDispose(teh);
paramPtr->returnValue = HStr("error : bad GetStylHandle()");
RestoreA4();
return;
}
in = (char **)TEGetText(teh);
if(in == 0){
TEDispose(teh);
paramPtr->returnValue = HStr("error : bad TEGetText()");
RestoreA4();
return;
}
NewList((char **) 0, &out);
AppendList(&out, "{\\rtf1\\mac");
/* figure out which fonts are used */
fonttable = 0;
for(run = 0; run < (*sh)->nRuns; run++){
stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;
if(findfonttable(fonttable, stp->stFont) < 0){
fonttable = addfonttable(fonttable, stp->stFont);
if(fonttable == 0){
oom:
TEDispose(teh);
paramPtr->returnValue = HStr("error : out of memory");
RestoreA4();
return;
}
}
}
if(fonttable == 0){
fonttable = addfonttable(fonttable, 0);
if(fonttable == 0)
goto oom;
}
/* spit out the list of fonts, using the Mac font numbers */
sprintf(tmp, "\\deff%u{\\fonttbl", (*fonttable)[0]);
AppendList(&out, tmp);
nfonts = GetHandleSize((Handle)fonttable) / sizeof(**fonttable);
for(i = 0; i < nfonts; i++){
GetFontName((*fonttable)[i], (StringPtr)fname);
PtoCstr(fname);
if(fname[0] == '\0')
sprintf(fname, "x%d", (*fonttable)[i]);
sprintf(tmp, "{\\f%u\\f%s %s;}",
(*fonttable)[i], fontfamily((*fonttable)[i]), fname);
AppendList(&out, tmp);
}
AppendList(&out, "}\r");
DisposHandle((Handle)fonttable);
run = -1;
inlen = (*teh)->teLength;
for(i = 0; i < inlen; i++){
if(run+1 < (*sh)->nRuns && i >= ((*sh)->runs)[run+1].startChar){
run++;
stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;
/* reset to plain style, then set the font and size */
sprintf(tmp, "\\plain\\f%u\\fs%d",
stp->stFont, stp->stSize * 2);
if(stp->stFace & bold)
strcat(tmp, "\\b");
if(stp->stFace & italic)
strcat(tmp, "\\i");
if(stp->stFace & underline)
strcat(tmp, "\\ulw");
if(stp->stFace & outline)
strcat(tmp, "\\outl");
if(stp->stFace & shadow)
strcat(tmp, "\\shad");
if(stp->stFace & 0x80) /* HyperCard's grouped text */
strcat(tmp, "\\hcgroup");
#if 0
if(stp->stFace & condense)
strcat(sname, "condense,");
if(stp->stFace & extend)
strcat(sname, "extend,");
#endif
strcat(tmp, " ");
AppendList(&out, tmp);
}
c = (*in)[i];
if(c >= ' ' && c < 0177 && c != '\\' && c != '{' && c != '}'){
AppendListChar(&out, c);
} else if(c == '\r'){
AppendList(&out, "\\par\r");
} else {
sprintf(tmp, "\\'%02x", c & 0xff);
AppendList(&out, tmp);
}
}
AppendList(&out, "}");
TEDispose(teh);
TrimList(&out);
if(out.h == 0){
paramPtr->returnValue = HStr("error : out of memory");
RestoreA4();
return;
}
paramPtr->returnValue = out.h;
RestoreA4();
}
Handle
HStr(str)
char *str;
{
Handle newHndl;
newHndl = (Handle) NewHandle((long) strlen(str) + 1);
if(newHndl == 0)
return(0);
strcpy((char *) (*newHndl), str);
return(newHndl);
}
int
findfonttable(tb, f)
int **tb; /* a table of fonts for RTF */
int f; /* the Mac font # */
{
int n, i;
n = GetHandleSize((Handle)tb) / sizeof(int);
for(i = 0; i < n; i++)
if(f == (*tb)[i])
return(i);
return(-1);
}
int **
addfonttable(tb, f)
int **tb;
int f; /* the Mac font # */
{
int n;
if(tb == 0){
tb = (int **)NewHandle(0L);
if(tb == 0)
return(0);
}
n = GetHandleSize((Handle)tb) / sizeof(**tb);
SetHandleSize((Handle)tb, (n + 1) * sizeof(**tb));
if(MemError() != 0){
DisposHandle((Handle)tb);
return(0);
}
(*tb)[n] = f;
return(tb);
}
char *
fontfamily(f)
int f;
{
switch(f){
case 0: case geneva: case helvetica:
return("swiss");
case newYork: case times:
return("roman");
case monaco: case courier:
return("modern");
case symbol:
return("tech");
default:
return("nil");
}
}